var base_url_user = '/rs/users';
var base_url_citys = '/rs/openCity';
var base_url_city = '/rs/city';
var base_url_user_category = '/rs/member_category';

var users = [];
var citys = [];
var operation = "add";
var currentPageNo = 1;
var pageRows = 10;

$(function() {
    authName = getCookie('username');
    userCity = getCookie('usercity');
    userCategory = getCookie('usercategory');

    if(userCategory == 9) {
        queryCitys();
        queryUsers(9);
    }
    else if(userCategory == 8){
        setCity();
        queryUsers(8);
    }

    $("#btnUserQuery").click(function() {
        currentPageNo = 1;
        if(userCategory == 9) {
            queryListAll();
        }
        else if(userCategory == 8){
            queryList();
        }
    });
});

function queryUsers(rank) {
    zhget(base_url_user_category, {
        id: '<|'+rank
    }, function(result) {
        var uList = $("#category");
        for (var al in result.rows) {
            var text = result.rows[al].name;
            var value = result.rows[al].id;
            uList.append("<option value='" + value + "'>" + text + "</option>");
        }
    });
}

function setCity() {
    zhget(base_url_city, {
        id:userCity
    }, function(result) {
        var cityList = $("#city_id");
        citys = result.rows;
        var text = result.rows[0].name;
        var value = userCity;
        cityList.append("<option value='" + value + "'>" + text + "</option>");
        queryList();
    });
}

function queryCitys() {
    zhget(base_url_citys, {
        sort:'orderid'
    }, function(result) {
        var cityList = $("#city_id");
        citys = result.rows;
        for (var al in citys) {
            var text = citys[al].name;
            var value = citys[al].id;
            cityList.append("<option value='" + value + "'>" + text + "</option>");
        }
        queryListAll();
    });
}

function queryListAll() {
    zhget(base_url_user, {
        username: $('#tisname').val(),
        shop_name:$('#shopName').val(),
        sort: 'id',
        order: 'desc',
        page: currentPageNo,
        category: '<|'+userCategory,
        size: pageRows
    }, function(result) {
        users = result.rows;
        for(var i = 0; i<users.length; i++) {
            for(var j = 0 ; j < citys.length ; j++){
                if(result.rows[i]['city_id']==citys[j]["id"]){
                    result.rows[i]['city_name'] = citys[j]['name'];
                    break;
                }
            }
        }
        buildTable(result, 'user-template', 'user-placeholder');
    });
}

function queryList() {
    zhget(base_url_user, {
        username: $('#tisname').val(),
        shop_name:$('#shopName').val(),
        city_id:userCity,
        sort: 'id',
        order: 'desc',
        category: '<|'+userCategory,
        page: currentPageNo,
        size: pageRows
    }, function(result) {
        users = result.rows;
        for(var i = 0; i<users.length; i++) {
            for(var j = 0 ; j < citys.length ; j++){
                if(result.rows[i]['city_id']==citys[j]["id"]){
                    result.rows[i]['city_name'] = citys[j]['name'];
                    break;
                }
            }
        }
        buildTable(result, 'user-template', 'user-placeholder');
    });
}

function onUserAddClick() {
    cleanForm();
    operation = "add";
    $("#userid").attr("readonly", "readonly");
    $("#userpwd").removeAttr("readonly");
    $("#shop_name").removeAttr("readonly");
    $('#userModal').modal('show');
}

function onUserUpdateClick(userid) {
    fillForm(userid);
    operation = "modify";
    $("#userid").attr("readonly", "readonly");
    $("#shop_name").attr("readonly", "readonly");
    $("#userpwd").attr("readonly", "readonly");
    $('#userModal').modal('show');
}

function onUserDeleteClick(userid) {
    if(confirm("确认要删除？")) {
        zhdelete(base_url_user + "/" + userid, {
            //AUTHNAME:authName
        }, function (result) {
            if (result && result.affectedRows > 0) {
                if(userCategory == 9) {
                    queryListAll();
                }
                else if(userCategory == 8){
                    queryList();
                }
                showSuccess('删除成功！');
            } else {
                showError('删除失败！');
            }
        });
    }
}

function onUserSaveClick() {
    var userid = $("#userid").val();
    var data = {
        AUTHNAME:authName,
        username: $("#username").val(),
        shop_name: $("#shop_name").val(),
        password: $("#userpwd").val(),
        mobile: $("#mobile").val(),
        email: $("#useremail").val(),
        category: $("#category").val(),
        city_id: $("#city_id").val()
    };
    if (operation == "add") {
        zhpost(base_url_user, data, saveResult);
    } else {
        zhput(base_url_user + "/" + userid, data, saveResult);
    }
}

function saveResult(result) {
    if (result) {
        if (result.outcome==0) {
            showError('保存失败！');
        } else{
            $('#userModal').modal('hide');
            if(userCategory == 9) {
                queryListAll();
            }
            else if(userCategory == 8){
                queryList();
            }
            showSuccess('保存成功！');
        }
    } else {
        showError('保存失败！');
    }
}

function fillForm(userid) {
    var index = 0;
    for (index in users) {
        var item = users[index];
        if (item.id == userid) {
            $("#userid").val(item.id);
            $("#shop_name").val(item.shop_name);
            $("#username").val(item.username);
            $("#userpwd").val('******');
            $("#mobile").val(item.mobile);
            $("#useremail").val(item.email);
            $("#category").val(item.category);
            $("#city_id").val(item.city_id);
            return;
        }
    }
}

function cleanForm() {
    $("#userid").val("");
    $("#shop_name").val("");
    $("#username").val("");
    $("#userpwd").val("");
    $("#mobile").val("");
    $("#useremail").val("");
}
